{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "df6816b9",
   "metadata": {},
   "source": [
    "* 2022/09/20/week05\n",
    "* python课堂笔记\n",
    "* 记录人：大阳"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1bd15cb",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "> 1.有序数据结构之列表\n",
    ">\n",
    "> 2.用户输入\n",
    ">\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3698d8db",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "# 思考 ：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考：处理数据会不会很麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fb019192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M,A,R,Y\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"M,A,R,Y\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0ae417e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'M,'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会麻烦？\n",
    "网三_name_list[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ce5e0dfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有几个人名?\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c83af21",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念\n",
    "* \n",
    "> 注释：我们已经学习了   数值   和    字符 ，list可以放数值和字符，但是通常要嘛一起数值，要嘛一起字符串\n",
    ">\n",
    "> 总结通用的有序数据结构的方法\n",
    ">> 1.取值：通过索引值取出对应内容\n",
    ">>\n",
    ">> 2.切片：通过索引值区间取出对应一些内容\n",
    ">>\n",
    ">> 3.in 可以查看成员对象是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9ed68f3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b6a0bef9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'C', 'D'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"A\",\"B\",\"C\",\"D\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d44419bb",
   "metadata": {},
   "source": [
    "### 1.1 列表运算法则（operation）\n",
    "* <font color= red>列表数据不做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f8cdfeb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'A'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中的第一位\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c3c2d600",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  多少位\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5e240de3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三:  A\n",
      "网三:  B\n",
      "网三:  C\n",
      "网三:  D\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三: \",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a2de3127",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查看数据是否存在\n",
    "\"J\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2cf18702",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in+ 切片 取值len（s）s.count）\n",
    "网三_name_list_转专业 = [\"w\",\"r\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d041c26a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', 'B', 'C', 'D', 'w', 'r']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b553b84a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考： 一个list 可不可以出现 重复项\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "b2edea59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfcd13ea",
   "metadata": {},
   "source": [
    "### 2.2 列表方法（function）--对列表数据进行操作\n",
    "> 1.增\n",
    ">\n",
    ">2.删\n",
    ">\n",
    ">3.改\n",
    ">\n",
    ">4.查\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e72e814e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c2a0e6e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"A\",\"B\",\"C\",\"D\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "b15fb25b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', 'B', 'C', 'D']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "60269a22",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list. append(\"G\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9c38521b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', 'B', 'C', 'D', 'G']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "990cbe97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ab6b41c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ed11a4bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "ed22a3ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', '李华', '李华', 'B', 'C', 'D', 'G']"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "e9ceec1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "76ba2243",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"B\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "76a5a80b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', '李华', '李华', 'C', 'D', 'G']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "769c6983",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', '李华']"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起用，先检查成员变量是否存在，然后再remove\n",
    "if \"C\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"C\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "0789f779",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', '李华']"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "4f85872c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "25d5ed24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "987d8bd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', '李华', '李华']"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "06c42fe5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,16,19,17,20]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "7c1b4a1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "1eb4b267",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 16, 19, 17, 20]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "acd96177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "b09ebbc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "803bd4ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index),先决定条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7718fd1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习：课本81页与68页"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
